<?php
error_reporting (E_ALL ^ E_NOTICE ^ E_WARNING ); 
require_once("core/SocialMashup.Plugins.php");
require_once("core/interface/IPlugins.php");

class SocialMashup {
	
	public $plugins = array();
	public $query;
	public $max_results;
		
	function __construct($query, $max_results = 20) {
		
		$this->query = $query;
		$this->max_results = $max_results;
		
		$dir = "plugins/";
		$dh = opendir($dir);

		while (($file = readdir($dh)) !== false) {
			
			if (preg_match('/php/', $file) != 0) {
				include( $dir . $file );
				$plugin = str_replace(".php", "", $file);				
				$this->plugins[strtolower($plugin)] = new $plugin($this->query, $this->max_results);	
			}
		}

		closedir($dh);
		
	}
	
	protected function create_general_object($item)
	{
		$smi = new SocialMashupItem($item);
	}
	
	protected function getGeneric()
	{
		
		$items = array();
		
		foreach ($this->plugins as $plugin => $obj)
		{	
			if (!isset($obj->output["items"]))
				continue;
			
			foreach ($obj->output["items"] as $item) {
				$sm = new SocialMashupItem($item);
				array_push($items, $sm);
				
			}
		}
		
		return $items;
	}
	
	public function exec($str) {
		
		return $this->plugins[$str];
	}
	
	public function getJSON()
	{
		$items = $this->getGeneric();
		$output = array();
		
		
		if ($items == null)
			return;
		
		$output["search"] = $this->query;
		$output["items"] = $items;
		
		echo(json_encode($output));
		
	}
	
	public function renderAll()
	{
		
		$items = $this->getGeneric();
		
		shuffle($items);
		
		$output = "<ul>";
		
		for ($i = 0; $i <= count($items); $i++)
		{
				
			$output .= "<li>";
			$output .= "<strong>" . $items[$i]->title . "</strong>";
			$output .= "<em>" . $items[$i]->type . "</em>";
			$output .= "</li>";
		}
		
		$output .= "</ul>";
		
		echo($output);
		
	}
	
}

class SocialMashupItem
{

	function __construct($item)
	{
		foreach($item as $key => $val)
			$this->$key = $val;
		 
		return $this;
	}
}

?>
